<?
include_once 'library.php';
?>
<!doctype html>
<html lang="ko">
<head>
	<? include('inc/head.php'); ?>
</head>
<body>
<?
$table_name = "test_result";
$select_array = array("*");

/* 한 페이지당 게시물 수 */
$pageRow = 20;

/* 중복 URL 체크박스 */
$uniqueCheck = $_GET['unique_check'];

/* 페이징 시작 값 */
if(!$_GET['page']){
	$cPage = "1";
	$pageStart = $cPage - 1;
}else{
	$cPage = $_GET['page'];
	$pageStart = ($cPage - 1) * $pageRow;
}

$limit = " LIMIT $pageStart, $pageRow";

/* 검색쿼리 처리 */
if($_GET['search_query'] != ""){
	$search_query = $_GET['search_query'];
	
	/* http scheme check */
	if(!strstr($search_query,"http://") && !strstr($search_query,"https://")){
		$search_query = "http://".$search_query;
	}
	
	$search_condition = "title LIKE '%$search_query%' OR url LIKE '$search_query%'";
	$condition = "$search_condition ORDER BY timestamp desc";
	$totalCount = count(selectDB($table_name, $select_array, $condition));
	$site_title = "<a href=\"$search_query\" target=\"_blank\">$search_query</a>";
}else{
	$condition = "1=1 ORDER BY timestamp desc";
	$resultCount = selectDB($table_name, $column_arr, $condition);
	$totalCount = selectDB($table_name, array("count(*)"), "1=1");
	$totalCount = $totalCount[0][0];
	$site_title = "전체 서비스";
}

/* 중복 URL 제거 */
if($uniqueCheck){
	$result = selectDBQ("SELECT * FROM (SELECT * FROM $table_name WHERE $condition ) AS pseudoTable GROUP BY pseudoTable.url ORDER BY pseudoTable.timestamp DESC $limit");
	$totalCount = count(selectDBQ("SELECT * FROM (SELECT * FROM $table_name WHERE $condition ) AS pseudoTable GROUP BY pseudoTable.url ORDER BY pseudoTable.timestamp DESC"));
	$score_sum = selectDBQ("SELECT SUM(scoreTable.score) FROM (SELECT * FROM (SELECT * FROM $table_name WHERE $condition ) AS pseudoTable GROUP BY pseudoTable.url ORDER BY pseudoTable.timestamp DESC) AS scoreTable");
}else{
	$result = selectDB($table_name, $select_array, $condition.$limit);
	$score_sum = selectDB($table_name, array('sum(score)'), $condition);
}

/* 서비스별 평균 점수 */
$score_sum = $score_sum[0][0];
$a11y_score = number_format($score_sum / $totalCount,2);

?>
	<div class="container-fluid">
		<div id="header" class="row-fluid">
			<? include_once('inc/header.php'); ?>
		</div>
		
		<div id="contents" class="row-fluid">
			<div class="span12">
				<h3 class="score_title"><?=$site_title?> 평균점수 <span class="num"><?=$a11y_score?>점</span></h3>
					
				<table class="score table table-bordered">
					<caption>사내 웹접근성 테스트 결과표</caption>
					<colgroup>
						<col scope="col" />
						<col scope="col" />
						<col scope="col" />
						<col scope="col" />
						<col scope="col" />
					</colgroup>
					<thead>
						<tr>
							<th>No</th>
							<th>Title</th>
							<th>URL</th>
							<th>Score</th>
							<th>Check Time</th>
						</tr>
					</thead>			
					<tbody>
					<?
					$no = $totalCount - $pageStart;
					for($i=0; $i < count($result); $i++){
						$url = $result[$i]['url'];
						$title = $result[$i]['title'];
						$score = $result[$i]['score'];
						$timestamp = $result[$i]['timestamp'];
						
						$checkdate = date("Y-m-d", $timestamp);
						$checktime = date("H:i:s", $timestamp);
						
						if($score >= 95){
							$trClass = "success";
						}elseif($score >= 80){
							$trClass = "warning";
						}else {
							$trClass = "error";
						}						
						
						echo '<tr class="'.$trClass.'"><td>'.$no.'</td><td>'.$title.'</td><td><a href="'.$url.'" title="'.$url.'" target="_blank">'.$url.'</a></td><td class="score">'.$score.'점</td><td class="time">'.$checkdate.'&nbsp;'.$checktime.'</td></tr>';
						$no--;
					}
					?>
					</tbody>
				</table>
				
				<form action="<?=$PHP_SELF?>" method="GET" class="form-search">
					<div class="input-append search_wrap">
						<div class="ucWrap" data-toggle="popover" tabindex="0">
							<input type="checkbox" name="unique_check" id="unique_check" <? if($uniqueCheck) echo 'checked=checked'; ?> /><label for="unique_check">중복 URL 제거</label>
						</div>
						<div class="keywordWrap">
							<label for="search_query">검색어</label>
							<input type="text" name="search_query" id="search_query" class="search-query" value="<?=$search_query?>" placeholder="전체목록은 빈 값 검색">
							<button type="submit" class="btn btn-info" title="검색"><i class="icon-search icon-white"></i></button>
						</div>
					</div>
					
				</form>
				<? paging($totalCount, $cPage, $pageRow); ?>
				<ul class="unstyled">
					<li><i class="icon icon-ok"></i> &nbsp; 95점 이상은 녹색, 80점 이상은 노란색, 그 이하는 빨간색으로 표시됩니다.</li>
					<li><i class="icon icon-ok"></i> &nbsp; 검색어 입력시 <strong>"http://", "https://"</strong>를 입력하셔야 정확한 검색결과를 제공합니다. 미 입력시 <strong>"http://"</strong>가 기본입니다.</li>
					<li><i class="icon icon-ok"></i> &nbsp; 측정 점수 및 평균 점수는 접근성 확보의 절대적 판단기준이 되지 않습니다.</li>
				</ul>
			</div>
		</div>
	</div>

	<div id="footer" class="row-fluid">
		<? include('inc/footer.php') ?>
	</div>
<script type="text/javascript">
$('.ucWrap').popover(
	{	
		trigger:'hover',
		placement:'top',
		content:'중복된 URL을 제거하고 가장 최신 검사 결과만 보여줍니다.'
	}
);

</script>
</body>
</html>